package com.hsd.znsh.controller.app;

import com.hsd.znsh.code.ValidateCodeProcessor;
import com.hsd.znsh.code.ValidateCodeProcessorHolder;
import com.hsd.znsh.entity.Opinion;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.enums.ResultEnum;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.input.APPRegisterUser;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.service.*;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-23 11:24:39
 */
@RequestMapping("/app/user")
@Controller
public class AppUserController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private MonitorService monitorService;

    @Autowired
    private UserService userService;

    @Autowired
    private ManagerSecondManagerService managerSecondManagerService;

    @Autowired
    private ValidateCodeProcessorHolder validateCodeProcessorHolder;

    @GetMapping("/login")
    @ResponseBody
    public ResultVO toLogin(String username,String password){
        if(StringUtils.isEmpty(username)) throw new ZnshException("用户名不能为空");
        if(StringUtils.isEmpty(password)) throw new ZnshException("密码不能为空");
        userService.findAppUserByUsername(username,password);
        return ResultVOUtil.success();
    }

    @PostMapping("/logout")
    @ResponseBody
    public ResultVO logout(@RequestHeader("token") String token) throws Exception {
        redisService.delete(token);
        return ResultVOUtil.success();
    }

    @PostMapping("/sendSmsCode")
    public void sendSmsCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ValidateCodeProcessor validateCodeProcessor=validateCodeProcessorHolder.findValidateCodeProcessor("sms");
        validateCodeProcessor.create(new ServletWebRequest(request, response));
    }

    @GetMapping("/getUserRole")
    @ResponseBody
    public ResultVO getUserRole(@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        return ResultVOUtil.success(commonService.getUserRole(user));
    }

    @PostMapping("/register")
    @ResponseBody
    public ResultVO register(@Valid APPRegisterUser appRegisterUser, BindingResult bindingResult) throws Exception {
        ValidatorUtil.valid(bindingResult);
        userService.registerAPPUser(appRegisterUser);
        return ResultVOUtil.success();
    }

    @PostMapping("/assignMonitor")
    @ResponseBody
    public ResultVO assignMonitor(String monitorSn,String alias,String hostSn){
        monitorService.assignMonitor(monitorSn,alias,hostSn);
        return ResultVOUtil.success();
    }

    @PostMapping("/unbindMonitor")
    @ResponseBody
    public ResultVO unbindMonitor(String monitorSn){
        monitorService.unbindMonitor(monitorSn);
        return ResultVOUtil.success();
    }

    @GetMapping("/getSecondManagerList")
    @ResponseBody
    public ResultVO getSecondManagerList(@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        List<User> list=managerSecondManagerService.getSecondManagerList(user);
        return ResultVOUtil.success(list);
    }

    @PostMapping("/addSecondManager")
    @ResponseBody
    public ResultVO addSecondManager(@RequestHeader("token") String token,@RequestParam(required = false) String managerName){
        if(StringUtils.isEmpty(managerName)) throw new ZnshException("二级管理员姓名不能为空");
        User user=redisService.get(token,User.class);
        managerSecondManagerService.addSecondManager(user,managerName);
        return ResultVOUtil.success("添加成功");
    }

    @PostMapping("/deleteSecondManager")
    @ResponseBody
    public ResultVO deleteSecondManager(@RequestHeader("token") String token,@RequestParam(required = false) String managerName){
        if(StringUtils.isEmpty(managerName)) throw new ZnshException("二级管理员姓名不能为空");
        User user=redisService.get(token,User.class);
        managerSecondManagerService.deleteSecondManager(user,managerName);
        return ResultVOUtil.success("删除成功");
    }

    @PostMapping("/feedback")
    @ResponseBody
    public ResultVO feedback(@RequestHeader("token") String token,@Valid Opinion opinion,BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        User user=redisService.get(token,User.class);
        userService.feedback(user,opinion);
        return ResultVOUtil.success("提交成功");
    }

    @PostMapping("/assignHost")
    @ResponseBody
    public ResultVO assignHost(String managerName,String ids){
        userService.assignHost(managerName,ids);
        return ResultVOUtil.success();
    }
}
